package com.dogstudio.touchblack.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import java.util.Random;


public class AppUtils {

    public static int getScreenWidth(Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        boolean isPortrait = displayMetrics.widthPixels < displayMetrics.heightPixels;
        return isPortrait ? displayMetrics.widthPixels
                : displayMetrics.heightPixels;
    }

    public static int getScreenHeight(Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        final Display display = windowManager.getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        boolean isPortrait = displayMetrics.widthPixels < displayMetrics.heightPixels;
        return isPortrait ? displayMetrics.heightPixels
                : displayMetrics.widthPixels;
    }

    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int getRandom() {
        Random random = new Random();
        int result = 0;
        int value = random.nextInt(100);
        if (value <= 20) {
            result = 0;
        } else if (value <= 55) {
            result = 1;
        } else if (value <= 80) {
            result = 2;
        } else {
            result = 3;
        }
        return result;
    }


}
